# -*- coding: utf-8 -*-
import scrapy
from urllib.parse import quote
from jingdongSelenuim.items import JingdongselenuimItem

class JingdongSpider(scrapy.Spider):
    name = 'jingdong'
    allowed_domains = ['list.jd.com']
    start_urls = ['http://list.jd.com/']
    base_url = 'https://search.jd.com/Search?keyword='

    def start_requests(self):
    	for keyword in self.settings.get('KEY_WORDS',''):
    		for page in range(1,self.settings.get('MAX_PAGE',1)+1):
    			url = self.base_url + quote(keyword)
    			yield scrapy.Request(url=url,meta={'page':page},callback=self.parse,dont_filter=True)


    def parse(self, response):
        products = response.css('#J_goodsList .gl-item')
        for _list in products:
        	item = JingdongselenuimItem()
        	item['name'] = _list.xpath("./div//div[contains(@class,'p-name')]//a//em//text()").extract()[2]
        	item['promo'] = _list.css('.p-name a>.promo-words::text').extract_first()
        	item['detailurl'] = _list.css('.p-img a::attr("href")').extract_first()
        	item['price'] = _list.css('.p-price strong>i::text').extract_first()  
        	item['comment'] = ''.join(str(k) for k in _list.xpath("./div//div[contains(@class,'p-commit')]//strong//text()").extract())       	
        	yield item

